// import { createClient } from '@supabase/supabase-js'
// import { readFile, writeFile } from 'fs/promises';

const { createClient } = require('@supabase/supabase-js');
const { readFile, writeFile } = require('fs/promises');


console.log()

const upload = process.argv[2].toLowerCase() === "true"
const srcPath = process.argv[3]
const destPath = process.argv[4]
const override = process.argv[5].toLowerCase() === "true"

const bucket = process.argv[6]

const projectUrl = process.argv[7]
const apiKey = process.argv[8]



const supabase = createClient(projectUrl, apiKey)

async function main() {
  const { data } = await supabase.storage.from('test').download('a.txt');
  // 其他逻辑...

if(upload)
{
  const content = await readFile(srcPath);

  const { data, error } = await supabase
    .storage
    .from(bucket)
    .upload(destPath, content, {
      upsert: override,
      // "Content-Type": "application/octet-stream"
    })

    if(error != null)
    {
      console.log(error)
      process.exit(1)
    }

}
else
{
  const { data, error } = await supabase
    .storage
    .from(bucket)
    .download(srcPath)

  if(error != null)
  {
    console.log(error)
    process.exit(1)
  }

  const arrayBuffer = await data.arrayBuffer(); // 先转为 ArrayBuffer
  const buffer = Buffer.from(arrayBuffer);

  await writeFile(destPath, buffer);
}
}

main().catch(err => console.error('执行失败:', err));